<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="http://117.80.114.220:81/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="http://117.80.114.220:81/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="roleMenuFrom" sec:authorize="hasAuthority('sys:role:menu')">
    <div class="layui-form-item">
        <label class="layui-form-label required">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" value="" class="layui-input" disabled="disabled">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">角色编码</label>
        <div class="layui-input-block">
            <input type="text" name="code" value="" class="layui-input" disabled="disabled">
        </div>
    </div>
</div>
<div class="layui-input-block" sec:authorize="hasAuthority('sys:role:menu')">
    <button id="role" class="layui-btn layui-btn-normal">保存授权</button>
</div>
<div id="roleMenu" style="margin: 20px auto;width: 1000px;overflow: hidden;" sec:authorize="hasAuthority('sys:role:menu')"></div>

<script src="http://117.80.114.220:81/static/lib/layui/layui.js" charset="utf-8"></script>
<script src="http://117.80.114.220:81/static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="http://117.80.114.220:81/static/js/my.js"></script>
<script>
    layui.use(['tree', 'util', 'form'], function () {
        var tree = layui.tree
            , layer = layui.layer
            , form = layui.form

        var roleId = getUrlParam("id");
        //加载数据
        myGet("/sys-role/findRole?id=" + roleId, null, function (data) {
            //给表单赋值
            if (data.code == 0) {
                form.val("roleMenuFrom", data.data);
            } else {
                layer.msg(data.msg);
            }
        });

        tree.render({
            elem: '#roleMenu'
            , data:initMenuData(roleId)
            , showCheckbox: true  //是否显示复选框
            , id: 'id'
        });

        $("#role").click(function(){
            //获得选中的节点
            console.info(tree.getChecked('id'));
            myPost("/sys-role-menu/save",JSON.stringify({roleId:roleId,menus:tree.getChecked('id')}),function(data){
                if(data.code == 0){
                    var alert = layer.alert("保存成功",{title:"提示"},function () {
                        // 关闭弹出层
                        layer.close(alert);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                }else{
                    var alert = layer.alert("授权失败:"+data.msg,{title:"提示"},function () {
                        // 关闭弹出层
                        layer.close(alert);
                    });
                }
            });
        });
    });
    function initMenuData(roleId){
        var result = {};
        myGet("/sys-role-menu/findRoleTree?id=" + roleId, null, function (data) {
            if (data.code == 0) {
                result = data.data;
            } else {
                layer.msg(data.msg);
            }
        });
        return result;
    }
</script>
</body>
</html>
